package com.wzu.servlet;

import com.wzu.pojo.Reader;
import com.wzu.pojo.User;
import com.wzu.service.ReaderService;
import com.wzu.service.ReaderServiceImpl;
import com.wzu.service.UserService;
import com.wzu.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/personalData")
public class PersonalDataServlet extends HttpServlet {
    private ReaderService readerService = new ReaderServiceImpl();
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            response.sendRedirect("login.jsp");
            return;
        }

        Reader reader = readerService.getReaderByUserId(user.getUser_id());
        request.setAttribute("reader", reader);
        System.out.println("Reader info: " + reader);

        String action = request.getParameter("action");
        if ("edit".equals(action)) {
            request.getRequestDispatcher("application/EditPersonalData.jsp").forward(request, response);
        } else {
            request.getRequestDispatcher("application/PersonalData.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            response.sendRedirect("login.jsp");
            return;
        }

        Reader reader = readerService.getReaderByUserId(user.getUser_id());
        if (reader == null) {
            request.setAttribute("error", "未找到您的读者信息，请联系管理员或补充资料");
            request.getRequestDispatcher("application/PersonalData.jsp").forward(request, response);
            return;
        }

        reader.setName(request.getParameter("name"));
        reader.setGender(request.getParameter("gender"));
        reader.setAge(Integer.parseInt(request.getParameter("age")));
        reader.setContact_info(request.getParameter("contact_info"));
        reader.setId_type(request.getParameter("id_type"));
        reader.setId_number(request.getParameter("id_number"));
        reader.setBorrow_limit(Integer.parseInt(request.getParameter("borrow_limit")));
        reader.setBorrow_period(Integer.parseInt(request.getParameter("borrow_period")));
        reader.setCan_renew(request.getParameter("can_renew") != null); // checkbox处理

        int updateResult = readerService.updateReader(reader);

        String newPassword = request.getParameter("newPassword");
        if (newPassword != null && !newPassword.isEmpty()) {
            userService.updatePassword(user.getUser_id(), newPassword);
        }

        if (updateResult > 0) {
            request.setAttribute("message", "信息更新成功！");
        } else {
            request.setAttribute("error", "个人信息修改失败");
        }

        // 更新完毕，跳回显示页面并刷新数据
        Reader updatedReader = readerService.getReaderByUserId(user.getUser_id());
        request.setAttribute("reader", updatedReader);
        request.getSession().setAttribute("reader", updatedReader); // 可选更新 session
        request.getRequestDispatcher("application/PersonalData.jsp").forward(request, response);
    }
}
